package com.bsoft.gol.hbs.service.impl;

import com.bsoft.gol.hbs.dao.PcnBaseTimeParamDAO;
import com.bsoft.gol.hbs.utils.BeanUtils;
import com.bsoft.gol.hbs.utils.CommonUtils;
import com.bsoft.gol.hbs.utils.DateUtils;
import ctd.controller.exception.ControllerException;
import ctd.net.rpc.Client;
import ctd.spring.boot.annotation.SsdevReference;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.bean.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import pcn.enums.OperationTypeEmun;
import pcn.service.PcnBaseParamService;
import pcn.service.PcnOperLogService;
import pcn.sign.PcnBaseTimeParam;
import pcn.vo.sign.PcnBaseTimeParamVo;
import service.rpc.BaseUserManageUnitIntf;

import java.util.*;

/**
 * @description: 家医基础配置
 */
@SsdevService("pcnBaseParamService")
public class PcnBaseParamServiceImpl implements PcnBaseParamService {

    @Autowired
    private PcnBaseTimeParamDAO pcnBaseTimeParamDAO;
    @SsdevReference("pcn.pcnOperLogService")
    private PcnOperLogService pcnOperLogService;

    @Autowired
    private BaseUserManageUnitIntf baseUserManageUnitService;

    @RpcService
    @Override
    public PcnBaseTimeParamVo queryPcnBaseParamByParamCode(String paramCode) {
        PcnBaseTimeParam pcnBaseTimeParam = pcnBaseTimeParamDAO.getPcnBaseParamByParamCode(paramCode);
        PcnBaseTimeParamVo pcnBaseTimeParamVo = BeanUtils.copyProperties(pcnBaseTimeParam, PcnBaseTimeParamVo.class);
        Long paramValueDay = 1L;
        Date computeDate = null;
        if ("DAY".equals(pcnBaseTimeParam.getParamUnit())) {//天
            computeDate = DateUtils.caclDate(new Date(), Calendar.DATE, -Integer.parseInt(pcnBaseTimeParam.getParamValue()));
        } else if ("YEAR".equals(pcnBaseTimeParam.getParamUnit())) {//年
            computeDate = DateUtils.caclDate(new Date(), Calendar.YEAR, -Integer.parseInt(pcnBaseTimeParam.getParamValue()));
        } else if ("MONTH".equals(pcnBaseTimeParam.getParamUnit())) {//月
            computeDate = DateUtils.caclDate(new Date(), Calendar.MONTH, -Integer.parseInt(pcnBaseTimeParam.getParamValue()));
        }
        if (computeDate != null) {
            paramValueDay = DateUtils.getBetweenDay(computeDate, new Date());
            pcnBaseTimeParamVo.setParamValueDay(paramValueDay);
        }
        return pcnBaseTimeParamVo;
    }

    @RpcService
    @Override
    public Integer save(PcnBaseTimeParam pcnBaseParam) throws ControllerException {
        //获取用户登录信息
        UserVo userVo = baseUserManageUnitService.getUserIdByOnline();
        pcnBaseParam.setTenantId(userVo.getTenantId());
        if (CommonUtils.isNull(pcnBaseParam.getParamId())) {
            pcnBaseParam.setIsEnable("1");
            pcnBaseTimeParamDAO.save(pcnBaseParam);
            //保存到记录表
            pcnOperLogService.saveInfo(pcnBaseParam.getParamName(), OperationTypeEmun.add.getName(), pcnBaseParam.getParamId().toString(), "系统配置-基础数据");
        } else {
            pcnBaseTimeParamDAO.updateById(pcnBaseParam.getParamId(), pcnBaseParam.getParamValue(), pcnBaseParam.getParamUnit());

            try {
                Client.rpcInvoke("pcn.pcnOperLogService","saveInfo",pcnBaseParam.getParamName(), OperationTypeEmun.update.getName(), pcnBaseParam.getParamId()!= null ? String.valueOf(pcnBaseParam.getParamId()):"", "系统配置-基础数据");
            } catch (Exception e) {
              throw new ControllerException("保存到记录表失败");
            }
            //保存到记录表
//            pcnOperLogService.saveInfo(pcnBaseParam.getParamName(), OperationTypeEmun.update.getName(), pcnBaseParam.getParamId()!= null ? String.valueOf(pcnBaseParam.getParamId()):"", "系统配置-基础数据");
        }
        return pcnBaseParam.getParamId();
    }


    @RpcService
    @Override
    public PcnBaseTimeParam getPcnBaseParamByParamCode(String s) {
        return pcnBaseTimeParamDAO.getPcnBaseParamByParamCode(s);
    }

    @RpcService
    @Override
    public List<PcnBaseTimeParam> queryPcnBaseParamByParamType(String s) {
        List<PcnBaseTimeParam> list = new ArrayList<PcnBaseTimeParam>();
        list = pcnBaseTimeParamDAO.queryPcnBaseParamByParamType(s);
        if (list != null && list.size() > 0) {
            for (PcnBaseTimeParam pcnBaseTimeParam : list) {
                pcnBaseTimeParam.setLevel("02");
                pcnBaseTimeParam.setModuleId("01");
            }
        }
        return list;
    }

    @RpcService
    @Override
    public Map<String, Object> queryPayByParamType(String paramType) {
        Map<String, Object> map = new HashMap<>();
        //增加是否添加支付
        List<PcnBaseTimeParam> pcnBaseTimeParamList = this.queryPcnBaseParamByParamType(paramType);
        for (PcnBaseTimeParam pcnBaseTimeParam : pcnBaseTimeParamList) {
            map.put(pcnBaseTimeParam.getParamCode(), pcnBaseTimeParam.getParamValue());
        }
        return map;
    }

    @RpcService
    @Override
    public Map<String, Object> queryPhotoByParamType(String paramType) {
        Map<String, Object> map = new HashMap<>();
        //增加是否必须拍照
        List<PcnBaseTimeParam> pcnBaseTimeParamList = this.queryPcnBaseParamByParamType(paramType);
        for (PcnBaseTimeParam pcnBaseTimeParam : pcnBaseTimeParamList) {
            map.put(pcnBaseTimeParam.getParamCode(), pcnBaseTimeParam.getParamValue());
        }
        return map;
    }

    @RpcService
    @Override
    public void saveList(List<PcnBaseTimeParam> pcnBaseParamList) throws ControllerException {
        if (pcnBaseParamList != null) {
            for (PcnBaseTimeParam pcnBaseParam : pcnBaseParamList) {
                this.save(pcnBaseParam);
            }
        }
    }

    @RpcService
    @Override
    public String getParamValue(String paramType,String paramCode){
        PcnBaseTimeParam pcnBaseParamByParamType = pcnBaseTimeParamDAO.getPcnBaseParamByParamType(paramType, paramCode);
        if(null != pcnBaseParamByParamType){
            return pcnBaseParamByParamType.getParamValue();
        }
        return null;
    }

    public PcnOperLogService getPcnOperLogService() {
        return pcnOperLogService;
    }

    public void setPcnOperLogService(PcnOperLogService pcnOperLogService) {
        this.pcnOperLogService = pcnOperLogService;
    }
}
